package com.blb.service.impl;

import com.blb.entity.*;
import com.blb.mapper.TransferMapper;
import com.blb.service.ITransferService;
import com.blb.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TransferServiceImpl implements ITransferService {
    @Resource
    private TransferMapper transferMapper;
    /*
    * 作者：钟宇轩
    * 用途：查询所有移库单号
    * */
    @Override
    public List<Transfer> findAll(int start, int length,String companyId) {
        return transferMapper.findAll(start,length,companyId);
    }

    @Override
    public int count(){
        return transferMapper.count();
    }
    /*
    * 作者：钟宇轩
    * 用途：条件查询所有要移库的商品
    * */
    @Override
    public List<IomWarehouse> search(String id, String iomGoods) {
        return transferMapper.search(id,iomGoods);
    }

    @Override
    public String findWareId(String ware) {
        return transferMapper.findWareId(ware);
    }

    /*
    * 作者：钟宇轩
    * 用途：查询所有仓库
    * */
    @Override
    public List<InventoryCheck> findAllWare(String companyId) {
        return transferMapper.findAllWare(companyId);
    }

    /*
    * 作者：钟宇轩
    * 用途：通过仓库查询所有库区
    * */
    @Override
    public List<Wasc> findAllArea(String id) {
        return transferMapper.findAllArea(id);
    }

    /*
    * 作者：钟宇轩
    * 用途：通过库区查询所有的货架
    * */
    @Override
    public List<Wasc> findAllShelves(int id) {
        return transferMapper.findAllShelves(id);
    }

    /*
    * 作者：钟宇轩
    * 用途：通过货架查询所有的货位
    * */
    @Override
    public List<Wasc> findAllCargo(String id) {
        return transferMapper.findAllCargo(id);
    }

    @Transactional
    @Override
    public void submission(MoveDetailList moveDetails, IomDetailList iomDetails, String person, String tel, String time, int number, String reason,String outwarehouse,String inwarehouse,String production,String companyId) {
        String iomNo = UUIDUtils.getId();
        /*
        * 从移出货位上删除当前商品，并在移入的货位上添加商品
        * */
        for(Search search : moveDetails.getMoveDetails()){
            String cargoNo = search.getCargoNo();
            String shelvesNo = search.getShelvesId();
            String goodsName = search.getGoodsName();
            int areaId = transferMapper.findAreaId(goodsName);
            String wareName = search.getWareName();
            String wareId = transferMapper.findWareId(wareName);
            Search oldData = transferMapper.getBatch(cargoNo, shelvesNo, areaId, wareId);
            transferMapper.emptyById(cargoNo, shelvesNo, areaId, wareId);
            for (IomWarehouse iom : iomDetails.getIomDetails()){
                if(iom.getIomGoodsno().equals(search.getLcode())){
                    String iomCargoloca = iom.getIomCargoloca();
                    String iomShelves = iom.getIomShelves();
                    String iomArea = iom.getIomArea();
                    String iomWare = transferMapper.findWareId(inwarehouse);
                    int batch = oldData.getBatch();
                    String iomGoods = iom.getIomGoods();
                    String productNo = oldData.getProductNo();
                    String lCode = oldData.getLcode();
                    transferMapper.addById(productNo,iomGoods,batch,iomNo,lCode,iomCargoloca,iomShelves,iomArea,iomWare);
                }
            }
        }

        /*
        *插入一条移库数据
        * */
        String wareName = transferMapper.findWareName(outwarehouse);
        transferMapper.insertMove(iomNo,iomNo,wareName,inwarehouse,production,number,reason,person,tel,time,companyId);

        /*
        * 插入移库详情表数据
        * */
        for(Search search : moveDetails.getMoveDetails()){
            String id = UUIDUtils.getId();
            String lCode = search.getLcode();
            String product = search.getProduct();
            String ware = search.getWareName();
            String area = search.getGoodsName();
            String shelves = search.getShelvesId();
            String cargo = search.getCargoNo();
            transferMapper.insertMoveDetails(id,lCode,product,ware,area,shelves,cargo,time,iomNo);
        }

        for(IomWarehouse iom : iomDetails.getIomDetails()){
            String id = UUIDUtils.getId();
            String lCode = iom.getIomGoodsno();
            String product = iom.getIomGoods();
            String area1 = iom.getIomArea();
            String area = transferMapper.findAreaName(area1);
            String shelves = iom.getIomShelves();
            String cargo = iom.getIomCargoloca();
            transferMapper.insertMoveDetails(id,lCode,product,inwarehouse,area,shelves,cargo,time,iomNo);
        }
    }

    @Override
    public Transfer moveRecord(String batch) {
        return transferMapper.moveRecord(batch);
    }

    @Override
    public List<IomWarehouse> outWare(String id, String outware) {
        return transferMapper.outWare(id,outware);
    }
}
